package site.wetsion.employ.server.service;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import site.wetsion.employ.server.mapper.CollectionMapper;
import site.wetsion.employ.server.mapper.CompanyMapper;
import site.wetsion.employ.server.mapper.JobMapper;
import site.wetsion.employ.server.mapper.UserMapper;
import site.wetsion.employ.server.pojo.CollectionCompany;
import site.wetsion.employ.server.pojo.CollectionJob;
import site.wetsion.employ.server.pojo.CollectionPost;
import site.wetsion.employ.server.pojo.Company;
import site.wetsion.employ.server.pojo.ForumPost;
import site.wetsion.employ.server.pojo.Job;
import site.wetsion.employ.server.pojo.JobRequestDetail;
import site.wetsion.employ.server.pojo.User;
import site.wetsion.employ.server.util.Page;

@Service
public class UserService {
	
	private final static Logger log = LoggerFactory.getLogger(UserService.class);

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private CollectionMapper collectionMapper;
	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private JobMapper jobMapper;
	
	/**
	 * 注册
	 * @param user
	 * @return 是否注册成功
	 */
	public boolean register(User user){
		try {
			userMapper.insertUser(user);
			return true;
		} catch (Exception e) {
			log.error("注册时插入数据失败", e);
			return false;
		}
	}
	
	/**
	 * 登录，如果用户密码正确，返回用户，否则返回空
	 * @param email 邮箱
	 * @param password 密码
	 * @return
	 */
	public User login(String email ,String password){
		try {
			User user = userMapper.findUserByEmail(email);
			if(user.getPassword().equals(password)){
				return user;
			}else{
				return null;
			}
		} catch (Exception e) {
			log.error("查询失败", e);
			return null;
		}
	}
	
	/**
	 * 根据email来查询是否已注册
	 * @param email
	 * @return
	 */
	public boolean checkRegister(String email){
		try {
			User user = userMapper.findUserByEmail(email);
			if(user!=null){
				return true;
			}
			return false;
		} catch (Exception e) {
			log.error("查询数据库时出错", e);
			return false;
		}
	}
	
	public User getUserById(BigInteger id){
		try {
			return userMapper.findUserById(id);
		} catch (Exception e) {
			log.error("根据id获取用户失败，id="+id, e);
			return null;
		}
	}
	
	/** 根据昵称模糊查询*/
	public List<User> getUserListByName(String keyword, Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<User> userList = userMapper.findUserByName(keyword,(pageNum-1)*pageSize, pageSize);
			for (User user : userList) {
				user.setRoleId(findRoleIdByUid(user.getId()));
			}
			return userList;
		} catch (Exception e) {
			return null;
		}
	}
	public int getCountOfUserListByName(String keyword){
		try {
			return userMapper.findCountOfUserByName(keyword);
		} catch (Exception e) {
			return 0;
		}
	}
	/** 根据用户id获取角色id*/
	public int findRoleIdByUid(BigInteger userId){
		try {
			return userMapper.findRoleByUserId(userId);
		} catch (Exception e) {
			return 0;
		}
	}
	
	
	/**  -----------------------收藏相关--------------------  */
	
	/** 添加职位收藏*/
	public boolean addJob2Collection(CollectionJob collection){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		collection.setCollection_job_status(0);
		collection.setGmt_create(now);
		collection.setGmt_modified(now);
		try {
			collectionMapper.insertCollectionJob(collection);
			return true;
		} catch (Exception e) {
			log.error("插入职位收藏失败", e);
			return false;
		}
	}
	
	/** 添加公司收藏*/
	public boolean addCompany2Collection(CollectionCompany collection){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		collection.setCollection_company_status(0);
		collection.setGmt_create(now);
		collection.setGmt_modified(now);
		try {
			collectionMapper.insertCollectionCompany(collection);
			return true;
		} catch (Exception e) {
			log.error("插入公司收藏失败", e);
			return false;
		}
	}
	
	/** 添加帖子收藏*/
	public boolean addPost2Collection(CollectionPost collection){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		collection.setCollection_post_status(0);
		collection.setGmt_create(now);
		collection.setGmt_modified(now);
		try {
			collectionMapper.insertCollectionPost(collection);
			return true;
		} catch (Exception e) {
			log.error("插入帖子收藏失败", e);
			return false;
		}
	}
	
	public CollectionJob getCollectionJob(BigInteger userid, BigInteger jobid){
		try {
			return collectionMapper.getCollectionJobByUidAndJobId(userid, jobid);
		} catch (Exception e) {
			log.error("根据用户id和职位id获取收藏失败", e);
			return null;
		}
	}
	public CollectionCompany getCollectionCompany(BigInteger userid, BigInteger companyid){
		try {
			return collectionMapper.getCollectionCompanyByUidAndCompanyId(userid, companyid);
		} catch (Exception e) {
			log.error("根据用户id和公司id获取收藏失败", e);
			return null;
		}
	}
	public CollectionPost getCollectionPost(BigInteger userid, BigInteger postid){
		try {
			return collectionMapper.getCollectionPostByUidAndPostId(userid, postid);
		} catch (Exception e) {
			log.error("根据用户id和公司id获取收藏失败", e);
			return null;
		}
	}
	
	/** 获取我的收藏帖子列表*/
	public List<ForumPost> getCollectionPostListByUid(BigInteger userId, Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<ForumPost> list = collectionMapper.getCollectionPostListByUid(userId, (pageNum-1)*pageSize, pageSize);
			Collections.sort(list);
			for (ForumPost forumPost : list) {
				User user = userMapper.findUserById(forumPost.getPost_starter_id());
				forumPost.setPost_starter(user);
			}
			return list;
		} catch (Exception e) {
			log.error("获取收藏帖子列表失败", e);
			return null;
		}
	}
	
	public int getCountOfMyCollectionPost(BigInteger userId){
		try {
			return collectionMapper.getCountOfCollectionPostByUid(userId);
		} catch (Exception e) {
			log.error("获取收藏帖子数量失败", e);
			return 0;
		}
	}
	
	/** 获取收藏公司列表*/
	public List<Company> getCollectionCompanyListByUid(BigInteger userId, Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<Company> companys = collectionMapper.getCollectionCompanyListByUid(userId, (pageNum-1)*pageSize, pageSize);
			for (Company company : companys) {
				String location_name = companyMapper.getLocationNameById(company.getCompany_location());
				System.out.println("a");
				company.setLocation_name(location_name);
				String domain_name = companyMapper.getDomainNameById(company.getCompany_domain());
				company.setDomain_name(domain_name);
			}
			return companys;
		} catch (Exception e) {
			log.error("获取收藏公司列表失败", e);
			return null;
		}
	}
	public int getCountOfMyCollectionCompany(BigInteger userId){
		try {
			return collectionMapper.getCountOfCollectionCompanyByUid(userId);
		} catch (Exception e) {
			log.error("获取收藏公司数量失败", e);
			return 0;
		}
	}
	
	/** 获取收藏职位列表*/
	public List<Job> getCollectionJobListByUid(BigInteger userId, Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<Job> jobs = collectionMapper.getCollectionJobListByUid(userId, (pageNum-1)*pageSize, pageSize);
			for (Job job : jobs) {
				JobRequestDetail detail = jobMapper.getJobDetailById(job.getJob_request_detail());
				job.setRequest_detail_content(detail.getRequest_content());
				String location_name = companyMapper.getLocationNameById(job.getJob_location());
				job.setLocation_name(location_name);
				String company_name = companyMapper.getCompanyById(job.getJob_company_id())
						.getCompany_name();
				job.setJob_company_name(company_name);
				String creater_name = userMapper.getCreaterNameById(job.getJob_creater_id());
				job.setCreater_name(creater_name);
				String domain_name = companyMapper.getDomainNameById(job.getJob_domain_id());
				job.setDomain_name(domain_name);
			}
			return jobs;
		} catch (Exception e) {
			log.error("获取收藏职位列表失败", e);
			return null;
		}
	}
	public int getCountOfMyCollectionJob(BigInteger userId){
		try {
			return collectionMapper.getCountOfCollectionJobByUid(userId);
		} catch (Exception e) {
			log.error("获取收藏职位数量失败", e);
			return 0;
		}
	}
	
	/** 取消收藏公司*/
	public boolean cancelCollectCompany(BigInteger userId, BigInteger companyId){
		try {
			collectionMapper.deleteCollectionCompany(userId, companyId);
			return true;
		} catch (Exception e) {
			log.error("取消收藏公司失败，公司id="+companyId, e);
			return false;
		}
	}
	/** 取消收藏职位*/
	public boolean cancelCollectJob(BigInteger userId, BigInteger jobId){
		try {
			collectionMapper.deleteCollectionJob(userId, jobId);
			return true;
		} catch (Exception e) {
			log.error("取消收藏招聘职位失败，职位id="+jobId, e);
			return false;
		}
	}
	/** 取消收藏帖子*/
	public boolean cancelCollectPost(BigInteger userId, BigInteger postId){
		try {
			collectionMapper.deleteCollectionPost(userId, postId);
			return true;
		} catch (Exception e) {
			log.error("取消收藏帖子失败，帖子id="+postId, e);
			return false;
		}
	}
	
	/** 添加用户的角色*/
	public boolean setUserRole(BigInteger userId, int roleId){
		try {
			userMapper.insertUserRole(userId, roleId);
			return true;
		} catch (Exception e) {
			log.error("插入用户角色失败", e);
			return false;
		}
	}
}
